home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_6.lha / 5_6 / 5_6a.c next >
Text File  |  1993-08-08  |  875b  |  68 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / implement a character queue using
  6. / a linked list
  7. include <char_queue.h>
  8. include <error.h>
  9.  
  10. har_queue:: char_queue(int)
  11.  
  12.    rear = front = 0;
  13.  
  14.  
  15. har_queue:: ~char_queue()
  16.  
  17.    this->clearQ();
  18.  
  19.  
  20. oid char_queue:: enqueue(char c)
  21.  
  22.    if (rear)
  23. {
  24. rear->next = new cQtable;
  25. rear = rear->next;
  26. }
  27.  
  28.    else
  29. front = rear = new cQtable;
  30.  
  31.    rear->next = 0;
  32.    rear->c = c;
  33.  
  34.  
  35. har char_queue:: dequeue()
  36.  
  37.    if (emptyQ())
  38. error("underflow of character queue");
  39.  
  40.    char c = front->c;
  41.    if (front == rear)
  42. {
  43. delete front;
  44. rear = front = 0;
  45. }
  46.  
  47.    else
  48. {
  49. cQtable *old = front;
  50. front = front->next;
  51. delete old;
  52. }
  53.  
  54.    return c;
  55.  
  56.  
  57. oid char_queue:: clearQ()
  58.  
  59.    while (front)
  60. {
  61. cQtable *old = front;
  62. front = front->next;
  63. delete old;
  64. }
  65.  
  66.    rear = 0;
  67.  
  68.